// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   Main.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package readwritewave2alaw;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import uoc.libcompression.G711.AlawDecoder;
import uoc.libcompression.G711.AlawEncoder;
import uoc.libwave.WaveFormatPCMShort;
// </editor-fold>

/**
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 2.0
 * @since 2013-08-04
 */
public class Main {

    /**
     * The main class.
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-14
     */
    public static void main(String[] args) {
        byte compressed;
        short expanded;
        short sample;

        if (args.length < 2) {
            System.err.println("Must supply filenames:");
            System.err.println("java -jar PCM-S16-LE-TestAlawCompression.jar"
                    + " input.wav output.wav");
            System.exit(1);
        }

        String strSrcFilePath = args[0];
        String strDstFilePath = args[1];

        try {
            // Load the wave file
            WaveFormatPCMShort waveData = new WaveFormatPCMShort(strSrcFilePath);

            // Encoder initalization
            AlawEncoder alawEncoder = new AlawEncoder();
            AlawDecoder alawDecoder = new AlawDecoder();
            short wChannel = (short) 1;
            // normalization factors
            short max = waveData.getMaxSample();
            double exp_max = Math.ceil(Math.log(max) / Math.log(2));
            double scaleA = Math.pow(2, 12) / Math.pow(2, exp_max);
            double scaleB = Math.pow(2, exp_max) / Math.pow(2, 12);

            for (int i = 0; i < waveData.getNumSamples(); i++) {
                sample = waveData.getSample(wChannel, i);
                // normalization of the input value
                sample = (short) (((double) sample) * scaleA);
                // get the compressed value
                compressed = alawEncoder.compress(sample);
                // expand the compressed value
                expanded = alawDecoder.expand(compressed);
                // normalization of the expanded value
                expanded = (short) (((double) expanded) * scaleB);
                // store the expanded element
                waveData.setSample(wChannel, i, expanded);
            }
            // save the wave file
            waveData.saveToFile(strDstFilePath);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
